//
//  MSSearchProjectViewController.m
//  SocietySale
//
//  Created by Mike on 16/11/17.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "MSSearchProjectViewController.h"
#import "MSSearchProjectTableVC.h"
#import "MSCreatProjectViewController.h"

@interface MSSearchProjectViewController ()
@property (nonatomic, strong) MSSearchBar            *searchBar;
@property (nonatomic, strong) MSSearchProjectTableVC *tableVC;
@property (nonatomic, strong) NSMutableArray         *dataArray;     //数据
@end

@implementation MSSearchProjectViewController

- (void)initNavigationBar{
    [super initNavigationBar];
    titleLable.text = @"选择项目";
    leftBarBtn.hidden = NO;
}

- (void)viewWillDisappear:(BOOL)animated{
    [self.searchBar resignFirstResponder];
}

- (void)initUI{
    self.tableVC = [[MSSearchProjectTableVC alloc] initWithStyle:UITableViewStyleGrouped];
    self.tableVC.view.frame = self.contentView.bounds;
    self.tableVC.enableRefresh = NO;
    self.tableVC.enableRequest = YES;
    self.tableVC.delegate = (id)self;
    self.tableVC.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.contentView addSubview:self.tableVC.view];
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREENFRAME.size.width, 60)];
    bgView.backgroundColor = kDefaultViewBackgroundColor;
    self.tableVC.tableView.tableHeaderView = bgView;
    
    self.searchBar = [[MSSearchBar alloc] initWithFrame:CGRectMake(0, 10, SCREENWIDTH, 40)];
    self.searchBar.placeholder = @"请输入项目名查找项目";
    self.searchBar.cornerRadius = 2;
    self.searchBar.delegate = (id)self;
    self.searchBar.placeholderColor = kDarkGrayColor;
    self.searchBar.borderColor = kNormalGray;
    self.searchBar.leftView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"search"]];
    [bgView addSubview:self.searchBar];
//    [self performBlock:^{
        [self.searchBar becomeFirstResponder];
//    } afterDelay:0.3];
}

- (void)testDataFunc{
    MSProjectModel *model = [[MSProjectModel alloc] init];
    model.title = @"上海浦东世和空港中心商业住房项目";
    model.projectType =  @"房地产";
    model.projectState =  @"主体施工";
    model.investment =  @"2亿";
    model.address =  @"上海";
    model.time =  @"2016.10.08";
    model.introduction =  @"项目为四栋单独塔楼，一座宾馆，一做酒店式办公楼，地下部分主题为一个小楼盘，整个项目投资回报率较高";
    
    MSProjectModel *model1 = [[MSProjectModel alloc] init];
    model1.title = @"苏州永华置业有限公司 意境鸣镝西部仓库";
    model1.projectType =  @"酒店";
    model1.projectState =  @"主体施工";
    model1.investment =  @"3亿";
    model1.address =  @"苏州";
    model1.time =  @"2016.10.08";
    model1.introduction =  @"项目为四栋单独塔楼，一座宾馆。";
    
    MSProjectModel *model2 = [[MSProjectModel alloc] init];
    model2.title = @"中国光大国际有限公司深圳办事处";
    model2.projectType =  @"市政公用";
    model2.projectState =  @"主体施工";
    model2.investment =  @"6亿";
    model2.address =  @"成都";
    model2.time =  @"2016.9.15";
    model2.introduction =  @"项目为四栋单独塔楼，一座宾馆，一做酒店式办公楼，地下部分主题为一个小楼盘，整个项目投资回报率较高";
    
    _dataArray = [@[model,model1,model2,model,model2,model1,model] mutableCopy];
    self.tableVC.haveData = YES;
}

#pragma mark - getData
- (void)updateViewController:(NSNotification *)note
{
    NSDictionary *responseDic = note.userInfo;
    if (responseDic[Receiver]==self.VctrID) {
        if (responseDic[@"ProjectSearchRelevance"]!=nil){
            if (responseDic[@"state"]!=nil){
                self.tableVC.totalPagesNum = [responseDic[@"totalPage"] intValue];
                if (self.tableVC.pageNum == 1) {
                    self.tableVC.dataArray = nil;
                }
                NSMutableArray *dataArrray = [[NSMutableArray alloc] initWithArray:self.tableVC.dataArray];
                [dataArrray addObjectsFromArray:responseDic[@"ProjectSearchRelevance"]];
                if (dataArrray.count) {
                    self.tableVC.haveData = YES;
                    self.tableVC.dataArray = dataArrray;
                }
                else{
                    self.tableVC.haveData = NO;
                    NSString *keyStr = [NSString stringWithFormat:@"直接创建“%@”项目",self.searchBar.text];
                    self.tableVC.dataArray = [NSMutableArray arrayWithArray:
                                              @[[DOKeyValue key:keyStr value:@""]]
                                              ];
                }
            }
            else{
//                self.tableVC.dataArray = self.tableVC.dataArray;
            }
        }
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.tableVC.haveData) {
        NSArray* viewControllers = [HDUtil checkNilArray:[rootNavigation viewControllers]];
        NSInteger selfIndex = [viewControllers indexOfObject:self];
        if (selfIndex-1>=0) {
            [viewControllers[selfIndex-1] removeFromParentViewController];
        }
        
        MSProjectModel *model = self.tableVC.dataArray[indexPath.row];
        [rootNavigation popViewControllerAnimated:YES];
        [[NSNotificationCenter defaultCenter] postNotificationName:KSelectedProject object:self userInfo:@{model.title:model.itemID}];
    }
    else{//创建项目
        MSCreatProjectViewController *vc = [[MSCreatProjectViewController alloc] init];
        vc.projectName = self.searchBar.text;
        [rootNavigation pushViewController:vc animated:YES];
    }
}

#pragma mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText{
    if ([[searchText stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] length]==0 || searchText.length<2) {//字符全是空格，不做操作
        
    }
    else{//无空字符时，开始匹配数据                               @"kw":searchText,
        NSDictionary *dict = @{@"provinces":@"",
                               @"citys":@"",
                               @"industrys":@"",
                               @"stages":@"",
                               @"investment":@"",
                               @"onlineTime":@"",
                               @"kw":searchText,
                               @"pageNo":@"1",
                               @"pageSize":@"15",
                               };
//        NSDictionary *dict = @{
//                               @"pageNo":@"1",
//                               @"pageSize":@"15",
//                               };
        [netEngine requestProjectSearchRelevanceWithTarget:self.VctrID hudView:self.contentView keyword:@"" params:dict];
    }
}

#pragma mark - scrollviewBeginDragging
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    [self.searchBar resignFirstResponder];
}

@end
